package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.come.Frame.ZhuFrame;
import org.come.bean.ChongjipackBean;
import org.come.bean.XXGDBean;
import org.come.entity.Goodstable;
import org.come.mouslisten.VipGoodsMouselistener;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.UserMessUntil;

import com.tool.btn.VipShopBtn;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;

public class EverydayRechargeGoodsJpanel extends JPanel {

	/** 图片 */
	private JLabel goodsImg;
	private JLabel goodsNum;
	private VipGoodsMouselistener mouselistener;

	/** 领取按钮 */
	private VipShopBtn getBtn;

	private ChongjipackBean chongjipackBean;
	/**
	 * 充值条件nameRich<br>
	 *
	 */
	private RichLabel nameRich;

	/** 最大数量 */
	private int maxNum;

	public EverydayRechargeGoodsJpanel() {
		this.setPreferredSize(new Dimension(562, 60));
		setOpaque(false);
		setLayout(null);

		getGoodsNum();
		getGetBtn();
		getGoodsImg();
		getNameRich();
	}

	/**
	 * 兑换物品
	 */
	public void exchangeGoods() {
		if (chongjipackBean == null) {
			return;
		}
		if (GoodsListFromServerUntil.Surplussum("-1", "-1", maxNum) < maxNum) {
			ZhuFrame.getZhuJpanel().addPrompt2("你的背包不够");
			return;
		}
		String sendmes = Agreement.getAgreement().PaydaygradesureAgreement("v" + chongjipackBean.getPackgradetype());
		SendMessageUntil.toServer(sendmes);
	}

	/** 展示物品 */
	public void showGoods(ChongjipackBean chongjipackBean) {
		this.chongjipackBean = chongjipackBean;
		maxNum = 0;
		if (chongjipackBean != null) {
			nameRich.setText("#W每日充值满#R" + chongjipackBean.getCanpaymoney() + "#W元");
			List<XXGDBean> goods = ChongjipackBean.getGoodsNew(chongjipackBean.getPackgoods());
			Goodstable goodstable = UserMessUntil.getgoodstable(goods.get(0).getId());
			for (int i = 0; i < goods.size(); i++) {
				maxNum += goods.get(i).getSum();
			}
			goodsNum.setText(goods.get(0).getSum() + "");
			goodsImg.setIcon(GoodsListFromServerUntil.imgpath2(goodstable.getSkin()));
//			mouselistener.setGoodstable(goodstable);
			goodsNum.setVisible(true);
			getBtn.setBtn(1);
		} else {
			nameRich.setText("");
			goodsImg.setIcon(null);
			goodsNum.setVisible(false);
//			mouselistener.setGoodstable(null);
			getBtn.setBtn(0);
		}
	}

	private ImageIcon icon;

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (icon == null) {
			icon = CutButtonImage.getImage("inkImg/old/background/S178.png", 562, 60);
		}
		g.drawImage(icon.getImage(), 0, 0, 562, 60, null);
	}

	public JLabel getGoodsImg() {
		if (goodsImg == null) {
			goodsImg = new JLabel();
			goodsImg.setOpaque(false);
			goodsImg.setBounds(10, 10, 43, 43);
//			mouselistener = new VipGoodsMouselistener();
//			goodsImg.addMouseListener(mouselistener);
			this.add(goodsImg);
		}
		return goodsImg;
	}

	public void setGoodsImg(JLabel goodsImg) {
		this.goodsImg = goodsImg;
	}

	public JLabel getGoodsNum() {
		if (goodsNum == null) {
			goodsNum = new JLabel();
			goodsNum.setBounds(38, 6, 16, 16);
			goodsNum.setIcon(CutButtonImage.getImage("inkImg/old/background/S24.png", -1, -1));
			goodsNum.setVerticalTextPosition(SwingConstants.CENTER);
			goodsNum.setHorizontalTextPosition(SwingConstants.CENTER);
			goodsNum.setForeground(Color.green);
			goodsNum.setFont(UIUtils.TEXT_FONT10);
			this.add(goodsNum);
		}
		return goodsNum;
	}

	public void setGoodsNum(JLabel goodsNum) {
		this.goodsNum = goodsNum;
	}

	public VipShopBtn getGetBtn() {
		if (getBtn == null) {
			getBtn = new VipShopBtn("inkImg/hongmu/1/lingqu.png", 1, 3, this);
			getBtn.setBounds(365, 17, 60, 26);
			this.add(getBtn);
		}
		return getBtn;
	}

	public void setGetBtn(VipShopBtn getBtn) {
		this.getBtn = getBtn;
	}

	public ChongjipackBean getChongjipackBean() {
		return chongjipackBean;
	}

	public void setChongjipackBean(ChongjipackBean chongjipackBean) {
		this.chongjipackBean = chongjipackBean;
	}

	public RichLabel getNameRich() {
		if (nameRich == null) {
			nameRich = new RichLabel("#W每日充值满#R1000#W元", UIUtils.TEXT_HY19);//TEXT_HY19			
			Dimension d = nameRich.computeSize(136);//136
			nameRich.setSize(d);
			nameRich.setPreferredSize(d);
			nameRich.setBounds(65, 15, 200, 30);
			this.add(nameRich);
		}
		return nameRich;
	}

	public void setNameRich(RichLabel nameRich) {
		this.nameRich = nameRich;
	}

	public VipGoodsMouselistener getMouselistener() {
		return mouselistener;
	}

	public void setMouselistener(VipGoodsMouselistener mouselistener) {
		this.mouselistener = mouselistener;
	}

	public int getMaxNum() {
		return maxNum;
	}

	public void setMaxNum(int maxNum) {
		this.maxNum = maxNum;
	}

}
